﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-4-26
/// Purpose: 		验证服务的WCF实现版本的客户端类
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2015-2-14       不使用异步的SignOut
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Description;

namespace RB.Web.Security.Wcf
{
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName = "RB.Web.Security.IAuthenticateService")]
    public interface IAuthenticateServiceClient : IAuthenticateService
    {
        [System.ServiceModel.OperationContractAttribute(IsOneWay = true, AsyncPattern = true, Action = "http://tempuri.org/IAuthenticateService/SignOut")]
        System.IAsyncResult BeginSignOut(string ticket, System.AsyncCallback callback, object asyncState);

        void EndSignOut(System.IAsyncResult result);

        [System.ServiceModel.OperationContractAttribute(IsOneWay = true, AsyncPattern = true, Action = "http://tempuri.org/IAuthenticateService/LogPasswordAttemp")]
        System.IAsyncResult BeginLogPasswordAttemp(string appCode, string username, string password, string ipAddress, string httpForwarded, System.AsyncCallback callback, object asyncState);

        void EndLogPasswordAttemp(System.IAsyncResult result);
    }

    /// <summary>
    /// 验证服务的WCF实现版本的客户端类
    /// </summary>
    public class WcfAuthenticateService : ClientBase<IAuthenticateServiceClient>, IAuthenticateServiceClient
    {
        private BeginOperationDelegate onBeginSignOutDelegate;

        private EndOperationDelegate onEndSignOutDelegate;

        private System.Threading.SendOrPostCallback onSignOutCompletedDelegate;

        private BeginOperationDelegate onBeginLogPasswordAttempDelegate;

        private EndOperationDelegate onEndLogPasswordAttempDelegate;

        private System.Threading.SendOrPostCallback onLogPasswordAttempCompletedDelegate;

        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> SignOutCompleted;

        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> LogPasswordAttempCompleted;

        public WcfAuthenticateService()
            : base(new ServiceEndpoint(ContractDescription.GetContract(typeof(IAuthenticateServiceClient)),
                new BasicHttpBinding(),
                new EndpointAddress(WcfSecurityConfigSection.Current.GetServiceAddress("AuthenticateService.svc"))))
        {
        }

        public UserSession GetSession(string appCode, string ticket, int userIsOnlineTimeWindow)
        {
            return base.Channel.GetSession(appCode, ticket, userIsOnlineTimeWindow);
        }

        public void SignOut(string ticket)
        {
            base.Channel.SignOut(ticket);
            //SignOutAsync(ticket);
        }

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        public System.IAsyncResult BeginSignOut(string ticket, System.AsyncCallback callback, object asyncState)
        {
            return base.Channel.BeginSignOut(ticket, callback, asyncState);
        }

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        public void EndSignOut(System.IAsyncResult result)
        {
            base.Channel.EndSignOut(result);
        }

        private System.IAsyncResult OnBeginSignOut(object[] inValues, System.AsyncCallback callback, object asyncState)
        {
            string ticket = ((string)(inValues[0]));
            return this.BeginSignOut(ticket, callback, asyncState);
        }

        private object[] OnEndSignOut(System.IAsyncResult result)
        {
            this.EndSignOut(result);
            return null;
        }

        private void OnSignOutCompleted(object state)
        {
            if ((this.SignOutCompleted != null))
            {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.SignOutCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }

        public void SignOutAsync(string ticket)
        {
            this.SignOutAsync(ticket, null);
        }

        public void SignOutAsync(string ticket, object userState)
        {
            if ((this.onBeginSignOutDelegate == null))
            {
                this.onBeginSignOutDelegate = new BeginOperationDelegate(this.OnBeginSignOut);
            }
            if ((this.onEndSignOutDelegate == null))
            {
                this.onEndSignOutDelegate = new EndOperationDelegate(this.OnEndSignOut);
            }
            if ((this.onSignOutCompletedDelegate == null))
            {
                this.onSignOutCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnSignOutCompleted);
            }
            base.InvokeAsync(this.onBeginSignOutDelegate, new object[] {
                        ticket}, this.onEndSignOutDelegate, this.onSignOutCompletedDelegate, userState);
        }

        public UserSession CreateSession(string appCode, string username, string ipAddress, string httpForwarded)
        {
            return base.Channel.CreateSession(appCode, username, ipAddress, httpForwarded);
        }

        public void LogPasswordAttemp(string appCode, string username, string password, string ipAddress, string httpForwarded)
        {
            LogPasswordAttempAsync(appCode, username, password, ipAddress, httpForwarded);
        }

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        public System.IAsyncResult BeginLogPasswordAttemp(string appCode, string username, string password, string ipAddress, string httpForwarded, System.AsyncCallback callback, object asyncState)
        {
            return base.Channel.BeginLogPasswordAttemp(appCode, username, password, ipAddress, httpForwarded, callback, asyncState);
        }

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        public void EndLogPasswordAttemp(System.IAsyncResult result)
        {
            base.Channel.EndLogPasswordAttemp(result);
        }

        private System.IAsyncResult OnBeginLogPasswordAttemp(object[] inValues, System.AsyncCallback callback, object asyncState)
        {
            string appCode = ((string)(inValues[0]));
            string username = ((string)(inValues[1]));
            string password = ((string)(inValues[2]));
            string ipAddress = ((string)(inValues[3]));
            string httpForwarded = ((string)(inValues[4]));
            return this.BeginLogPasswordAttemp(appCode, username, password, ipAddress, httpForwarded, callback, asyncState);
        }

        private object[] OnEndLogPasswordAttemp(System.IAsyncResult result)
        {
            this.EndLogPasswordAttemp(result);
            return null;
        }

        private void OnLogPasswordAttempCompleted(object state)
        {
            if ((this.LogPasswordAttempCompleted != null))
            {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.LogPasswordAttempCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }

        public void LogPasswordAttempAsync(string appCode, string username, string password, string ipAddress, string httpForwarded)
        {
            this.LogPasswordAttempAsync(appCode, username, password, ipAddress, httpForwarded, null);
        }

        public void LogPasswordAttempAsync(string appCode, string username, string password, string ipAddress, string httpForwarded, object userState)
        {
            if ((this.onBeginLogPasswordAttempDelegate == null))
            {
                this.onBeginLogPasswordAttempDelegate = new BeginOperationDelegate(this.OnBeginLogPasswordAttemp);
            }
            if ((this.onEndLogPasswordAttempDelegate == null))
            {
                this.onEndLogPasswordAttempDelegate = new EndOperationDelegate(this.OnEndLogPasswordAttemp);
            }
            if ((this.onLogPasswordAttempCompletedDelegate == null))
            {
                this.onLogPasswordAttempCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnLogPasswordAttempCompleted);
            }
            base.InvokeAsync(this.onBeginLogPasswordAttempDelegate, new object[] {
                        appCode,
                        username,
                        password,
                        ipAddress,
                        httpForwarded}, this.onEndLogPasswordAttempDelegate, this.onLogPasswordAttempCompletedDelegate, userState);
        }
    }
}
